<!DOCTYPE html>
<html lang="en">
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no">
    <title>签到</title>
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/message.css"/>
    <link rel="stylesheet" href="/css/like.css"/>
    <link rel="stylesheet" href="../js/plugins/jquery-confirm/css/jquery-confirm.css">
    <script src="../js/plugins/jquery-confirm/js/jquery-confirm.js"></script>
    <script src="../js/plugins/jrender/jrender.min.js"></script>
    <script src="../js/plugins/dialog2/dialog.min.js"></script>
    <link rel="stylesheet" href="../js/plugins/dialog2/dialog.css">
    <link rel="stylesheet" href="../css/public.css">
    <link rel="stylesheet" href="../css/signin.css">
    <script src="../js/rili/rili.js"></script>
</head>
<script>
    $(function () {
        var users = sessionStorage.getItem("user");
        var user = JSON.parse(users);
        $.get("/signs/" + user.id,function (data) {
            $(".integral").renderValues(data);
        });
    })
</script>
<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col">
            <a href="javascript:history.go(-1);">
                <span><i class="fa fa-chevron-left"></i></span>
            </a>
        </div>
        <div class="col">
            <span>日历签到</span>
        </div>
        <div class="col"></div>
    </div>
</div>
<div class="top flex flex-align-end flex-pack-center flex-warp">
    <div class="out-1 flex flex-align-center flex-pack-center" id="signIn">
        <div class="out-2 flex flex-align-center flex-pack-center">
            <div class="signBtn">
                <strong id="sign-txt">连续签到</strong>
                <span>连签<em id="sign-count">0</em>天</span>
            </div>
        </div>
    </div>
</div>
<div class="tips">今天已签到，加油么么哒</div>
<div class="Calendar">
    <div id="toyear" class="flex flex-pack-center">
        <div id="idCalendarPre">&lt;</div>
        <div class="year-month">
            <span id="idCalendarYear">2018</span>年<span id="idCalendarMonth">6</span>月
        </div>
        <div id="idCalendarNext">&gt;</div>
    </div>
    <table border="1px" cellpadding="0" cellspacing="0">
        <thead>
        <tr class="tou">
            <td>日</td>
            <td>一</td>
            <td>二</td>
            <td>三</td>
            <td>四</td>
            <td>五</td>
            <td>六</td>
        </tr>
        </thead>
        <tbody id="idCalendar">
        </tbody>
    </table>
    <div class="integral">您当前可用积分:<a style="color: darkorange" render-html="integral" href="/mine/store/stroepersonal.html"></a></div>
    <div class="rltit" style="text-align: center;">签到规则</div>
    <div class="rl-main gz">
        <div class="item-list">
            <div class="item" style="text-align: center;">签到一天得10积分</div>
            <div class="item" style="text-align: center;">连续签到第八天给50积分</div>
        </div>
    </div>
</div>

<script language="JavaScript">

    var users = sessionStorage.getItem("user");
    var user = JSON.parse(users);

    $.get("/signs/" + user.id + "/time", function (data) {
        var timestamp;
        var myday=[]; //已签到的数组
        $.each(data, function (index, value) {
            timestamp = Date.parse(value);
            timestamp = timestamp / 1000;


            var todayYear = (new Date()).getFullYear();
            var todayMonth = (new Date()).getMonth();
            var todayDay = (new Date()).getDate();
            var todayTime = (new Date(todayYear, todayMonth, todayDay, '23', '59', '59')).getTime();//毫秒
            var isSign = false;
            myday[index] = timestamp

            //计算连续签到的天数
            function allCale(arr) {


                var count = 1;
                for (var i = arr.length - 2; i >= 0; i--) {
                    var next = parseInt(arr[i]) + 86400;
                    var final = getFiByTime(next);
                    if (arr[i + 1] < final) {
                        count++;
                    } else {
                        break;
                    }
                }
                return count;
            };

            //辅助计算连续签到的天数
            function getFiByTime(time) {
                time = parseInt(time) * 1000;
                var todayYear = (new Date(time)).getFullYear();
                var todayMonth = (new Date(time)).getMonth();
                var todayDay = (new Date(time)).getDate();
                var todayTime = (new Date(todayYear, todayMonth, todayDay, '23', '59', '59')).getTime();//毫秒
                return parseInt(todayTime) / 1000;
            };
            var cale
            $.get("/signs/" + user.id,function (data) {
                cale = new Calendar("idCalendar", {
                    qdDay: myday,
                    onToday: function (o) {
                        o.className = "onToday";
                    },
                    onSignIn: function () {
                        $$("sign-txt").innerHTML = '已签到';
                    },

                    onFinish: function () {
                        $$("sign-count").innerHTML = data.signs; //已签到次数
                        $$("idCalendarYear").innerHTML = this.Year;
                        $$("idCalendarMonth").innerHTML = this.Month; //表头年份

                    }
                });
            })
            $$("idCalendarPre").onclick = function () {
                cale.PreMonth();
            };
            $$("idCalendarNext").onclick = function () {
                cale.NextMonth();
            };
            //添加今天签到
            $$("signIn").onclick = function () {
                if (isSign == false) {
                    var res = cale.SignIn();
                    if (res == '1') {
                        $.post("/signs/" + user.id,function (data) {
                            if(data.success){
                                window.location.reload();
                            }else {
                                $.alert(data.msg)
                            }
                        });
                        $$("sign-txt").innerHTML = '已签到';
                        $$("sign-count").innerHTML = parseInt($$("sign-count").innerHTML) + 1;
                        isSign = true;
                    } else if (res == '2') {
                        $$("sign-txt").innerHTML = '已签到';
                        $(document).dialog({
                            type : 'confirm',
                            style: 'IOS',
                            titleShow: false,
                            content: '你已签到,请勿重复签到',
                            buttons: [
                                {
                                    name: '确认',
                                    callback: function() {
                                        window.location.reload();
                                    }
                                }
                            ]
                        });
                    }
                } else {
                    $(document).dialog({
                        type : 'confirm',
                        style: 'IOS',
                        titleShow: false,
                        content: '你已签到,请勿重复签到',
                        buttons: [
                            {
                                name: '确认',
                                callback: function() {
                                    window.location.reload();
                                }
                            }
                        ]
                    });
                }

            }
        });
    });
</script>
</body>

</html>